/******************************************************************************
* SVN­Version­Information:
* ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
* $LastChangedRevision: 6 $
* $LastChangedBy: c.a.duevel $
* $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
* $HeadURL: http://robofight.googlecode.com/svn/trunk/ClientJavaFX/src/de/hft_stuttgart/robofight/client/javafx/Main.fx $
******************************************************************************/
package de.hft_stuttgart.robofight.client.javafx;

import de.hft_stuttgart.botwar.common.interfaces.IRemotePlayer;
import de.hft_stuttgart.robofight.client.java.JPlayer;
import de.hft_stuttgart.robofight.client.javafx.util.*;
import java.util.ArrayList;
import javafx.reflect.FXLocal;
import javafx.reflect.FXObjectValue;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.image.Image;

/**
 * @author ralf.dauenhauer@gmx.de, michael.rieker@web.de
 */
class MainFrame {
    var menueNodes = new ArrayList();
    var currentNode : Node;
    var fxzLoader = new FxzLoader();
    var audioPlayer = new AudioPlayer();
    var jPlayer : JPlayer = null;
    var background: Node;
    var context : FXLocal.Context ;
    var newClassType : FXLocal.ClassType;
    var objVal : FXObjectValue;

    init {
        context = FXLocal.getContext();
    }
    
    /**
     * returns the fxzLoader Object, which contains all nodes
     * @return FxzLoader
     */
    function getFxzNodes() : FxzLoader{
        return this.fxzLoader;
    }


    function getNodeByName(nodeName : String) : Node {
        this.fxzLoader.getNodeByName(nodeName);
    }

    function playSoundByName(soundName : String) {
          audioPlayer.play(soundName);
    }

    /**
     * creates a new jPlayer Object
     * @param IRemotePlayer
     */
    function createJPlayer(paramPlayer : IRemotePlayer) : Boolean {
        if (paramPlayer != null){
            jPlayer = new JPlayer(paramPlayer);
            return true;
        } else {
            return false;
        }
    }

    function getParam() : String {
        return serverIP;
    }

    /**
     * returns the JPlayer object (contains basic methods and IRemotePlayer)
     * @return JPlayer
     */
    function getJPlayer() : JPlayer {
        return this.jPlayer;
    }

    /**
     * changes the content of the main scene
     * @param classname - String
     */
    function loadNewCustomNode(className : String) : Void {
        newClassType = context.findClass("de.hft_stuttgart.robofight.client.javafx.{className}");
        objVal = newClassType.allocate();
        if(not (className == "Register" or className == "Login")){
            objVal.initVar("getPlayer", context.mirrorOf(getJPlayer));
        }
        if(className == "Login"){
            objVal.initVar("createPlayer", context.mirrorOf(createJPlayer));
            objVal.initVar("getParam", context.mirrorOf(getParam));
        }
        if(className == "Register"){
            objVal.initVar("getParam", context.mirrorOf(getParam));
        }
        if(className == "Arena"){
            objVal.initVar("playSoundByName", context.mirrorOf(playSoundByName));
            frame.background.toBack();
        }
        objVal.initVar("getNodeByName", context.mirrorOf(getNodeByName));
        objVal.initVar("switchNode", context.mirrorOf(loadNewCustomNode));
        objVal.initialize();
        audioPlayer.play("menue");
        delete currentNode from maingroup.content;
        currentNode = ((objVal as FXLocal.ObjectValue).asObject()) as CustomNode;
        delete maingroup.content;
        insert currentNode into maingroup.content;
        objVal = null;
        newClassType = null;
        }
}


var serverIP : String = "";
    var frame = MainFrame {

    }
public function run(__ARGS__:String[]){

    serverIP = __ARGS__[0];
    // set first currentNode to login
    frame.audioPlayer.initialize(serverIP);
    frame.fxzLoader.initialize(serverIP);
   // frame.background = frame.getNodeByName("background");
    frame.loadNewCustomNode("Login");
}

/**
 * the main Stage for the whole application
 */

 var maingroup = Group { };

 function logout(){
        try{
            frame.jPlayer.logout();
            java.lang.System.out.println("Spieler wurde ausgeloggt!!");
        } catch(any){
            java.lang.System.out.println(any.getMessage());
        } finally {
            java.lang.System.exit( 0 );
        }
 }

var stage : Stage = Stage {
    title: Constants.STAGE_TITLE
    resizable: false;
    onClose: logout
    icons:  Image{ url:  "http://robofight.hft-stuttgart.de/_res/img/robofight_icon.JPG" }
    scene : Scene {
          height: Constants.SCENE_HEIGHT
          width : Constants.SCENE_WIDTH
          fill: Constants.SCENE_BACKGROUND_COLOR
          content: [ frame.background, maingroup ];
    }
 }
